#include "common.h"
#include "electric_motor.hpp"
#include "environment.hpp"
#include "locomotives.hpp"
#include "railroad_car.hpp"
#include "railroad_operators.hpp"
#include "route.hpp"
#include "tech_spec_lib.hpp"
#include "track.hpp"
#include "train.hpp"
#include <cassert>
#include <iostream>
#include <vector>
// 测试车辆类

Train *createTrain(RailroadOperator *railroadOperator, Route *route) {

    // 创建车厢
    std::vector<RailroadWagon *> wagons;
    for (int i = 0; i < 40; i++) {
        RailroadWagon *wagon =
            railroadOperator->assets->createWagon("N6", "N6-1");
        wagon->setLoad(60);
        wagons.push_back(wagon);
    }
    // 创建机车
    ElectricLocomotive *electricLocomotive =
        (ElectricLocomotive *)railroadOperator->assets->createLocomotive(
            "HXD3C", "hxd3c-1");
    // 创建列车
    Train *train =
        new Train(std::vector<Locomotive *>{electricLocomotive}, wagons);
    train->setRoute(route);
    return train;
}

// 带有火车的综合测试流程
void test_train() {

    RailroadOperator *railroadOperator = new RailroadOperator("Demo");
    Track *track1 = railroadOperator->assets->createTrack(2000, 80);
    Track *track2 = railroadOperator->assets->createTrack(3000, 80);
    Track *track3 = railroadOperator->assets->createTrack(3000, 100);
    Track *track4 = railroadOperator->assets->createTrack(3000, 80);
    Route *route =
        new Route(std::vector<Track *>{track1, track2, track3, track4},
                  std::vector<DIRECTION>{DIRECTION::DOWN, DIRECTION::DOWN,
                                         DIRECTION::DOWN, DIRECTION::DOWN},
                  4);
    Train *train1 = createTrain(railroadOperator, route);
    Train *train2 = createTrain(railroadOperator, route);
    std::cout << "train length: " << train1->getLength() << std::endl;
    // 设置路线

    std::cout << "Train route length: " << route->items.size() << std::endl;
    train1->trainDynamics.state = TRAIN_STATE::ACCELERATING;
    train2->trainDynamics.state = TRAIN_STATE::WAITING;
    for (int i = 0; i < 620; i++) {
        train1->update();
        train2->update();
        if (i == 100) {
            train2->trainDynamics.state = TRAIN_STATE::ACCELERATING;
            
        }
        std::cout << i << std::endl;
        environment.t += 1;
    }
}

int main() {

    test_train();
    return 0;
}
